/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.CryptoUtil;
import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysUser;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-11-27
 */
@Service("sysuserService")
public class SysUserServiceImpl implements SysUserService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysUser
     ****************************************/
    public void insert(SysUser sysuser) throws Exception {
        String userId = TemplateUtil.genUUID();
        sysuser.setUserId(userId);
        sysuser.setSalt(CryptoUtil.getRandomSalt());
        String password = CryptoUtil.encodePassphrase(sysuser.getPassword(), sysuser.getSalt());
        sysuser.setPassword(password);
        iDataSet4iBatis.insert(sysuser);
    }

    /****************************************
     * Update a instace of SysUser
     ****************************************/
    public void update(SysUser sysuser) throws Exception {
        sysuser.setSalt(CryptoUtil.getRandomSalt());
        String password = CryptoUtil.encodePassphrase(sysuser.getPassword(), sysuser.getSalt());
        sysuser.setPassword(password);
        iDataSet4iBatis.update(sysuser);
    }

    /****************************************
     * Delete a instace of SysUser
     ****************************************/
    public void delete(SysUser sysuser) throws Exception {
        iDataSet4iBatis.delete(sysuser);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(SysUser sysuser) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysUser sysuser) throws Exception {
        String statementId = sysuser.getStatmentId();
        String mainId = sysuser.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> sysuserList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysUser model = new SysUser();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            sysuserList.add(model);
        }
        iDataSet4iBatis.batchDelete(sysuserList);
    }

    /****************************************
     * Query instaces of SysUser
     ****************************************/
    public List<BaseModel> queryAsList(SysUser sysuser) throws Exception {
        String queryListStatement = "";
        if (sysuser.isPaging()) {
            queryListStatement = sysuser.getStatmentId();
            sysuser.setStatmentId("SysUser.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(sysuser);
            sysuser.setTotalCount(baseModel.getTotalCount());
            sysuser.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(sysuser);
    }

    /****************************************
     * Query tree of SysUser
     ****************************************/
    public List<BaseModel> queryUserAsTree(SysUser sysuser) throws Exception {
        List<BaseModel> baseModels = iDataSet4iBatis.queryAsList(sysuser);
        for (BaseModel baseModel : baseModels) {
            SysUser user = (SysUser)baseModel;
            user.setIcon("resources/MEDIA/images/person_sel.png");
        }
        return baseModels;
    }

    /****************************************
     * Query a instaces of SysUser
     ****************************************/
    public BaseModel queryAsObject(SysUser sysuser) throws Exception {
        return iDataSet4iBatis.queryAsObject(sysuser);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }

}
